package com.hiwan.unions.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.yanzhenjie.nohttp.Logger;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by llg on 2017/7/6.
 */
public class ProgressWebView extends WebView {
    private ScrollViewListener scrollViewListener = null;

    public ProgressWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //允许JS执行
        getSettings().setJavaScriptEnabled(true);
        getSettings().setCacheMode(getSettings().LOAD_NO_CACHE);
        // 开启 DOM storage API 功能
        getSettings().setDomStorageEnabled(true);
        getSettings().setDefaultTextEncodingName("utf-8");
        getSettings().setAllowFileAccess(true); // 允许访问文件
        getSettings().setLoadWithOverviewMode(true);
        //自适应屏幕
        getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        // 设置可以支持缩放
        getSettings().setSupportZoom(true);
        // 设置出现缩放工具
        getSettings().setBuiltInZoomControls(true);
        //扩大比例的缩放
        getSettings().setUseWideViewPort(true);
        getSettings().setDisplayZoomControls(false);

    }

    public interface ScrollViewListener {
        void onScrollChanged(ProgressWebView scrollView, int x, int y,
                             int oldx, int oldy);

    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
        }
    }

    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    public void quickCallJs(String method, String... params) {
        StringBuilder sb = new StringBuilder();
        sb.append("javascript:" + method);
        if (params == null || params.length == 0) {
            sb.append("()");
        } else {
            sb.append("(").append(concat(params)).append(")");
        }
        Logger.i("js" + sb.toString());
        loadUrl(sb.toString());
    }

    public boolean isJson(String target) {
        if (TextUtils.isEmpty(target))
            return false;
        boolean tag = false;
        try {
            if (target.startsWith("["))
                new JSONArray(target);
            else
                new JSONObject(target);

            tag = true;
        } catch (JSONException igonre) {
            tag = false;
        }

        return tag;

    }

    private String concat(String... params) {
        StringBuilder mStringBuilder = new StringBuilder();
        for (int i = 0; i < params.length; i++) {
            String param = params[i];
            if (!isJson(param)) {
                mStringBuilder.append("\"").append(param).append("\"");
            } else {
                mStringBuilder.append(param);
            }

            if (i != params.length - 1) {
                mStringBuilder.append(" , ");
            }

        }

        return mStringBuilder.toString();
    }
}
